/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.rsc.feeds.client.gui;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.Entry;
import hu.rsc.feeds.client.util.EmailValidator;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

/**
 * 
 * @author arbuckle
 */
public class PostPopup extends DialogBox {
    public PostPopup(final Entry entry, String dialogtitle) {
	super(true);
	setText(dialogtitle);
	DockPanel main = new DockPanel();
	main.addStyleName("popup");

	Label title = new Label(entry.getTitle());
	main.add(title, DockPanel.NORTH);

	HTML description = new HTML();
	String html = "<div>" + (entry.getHtmlDescription() != null ? entry.getHtmlDescription() : entry.getDescription()) + "</div>";
	description.setHTML(html);
	main.add(description, DockPanel.CENTER);

	HorizontalPanel linkpanel = new HorizontalPanel();

	Hyperlink link = new Hyperlink();
	link.addStyleName("link");
	link.setText(Dictionary.getEntry(Dictionary.LABEL_DETAILS_LINK, null));
	link.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Window.open(entry.getLink(), entry.getTitle(), null);
	    }
	});
	linkpanel.add(link);

	Hyperlink sendLink = new Hyperlink();
	sendLink.addStyleName("link");
	sendLink.setText(Dictionary.getEntry(Dictionary.LABEL_SHARE_LINK, null));
	sendLink.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		String mail = Window.prompt(Dictionary.getEntry(Dictionary.MESSAGE_SHARE_PROMPT, null), "");
		if (mail != null) {
		    List<String> recipients = checkMailAddresses(mail);
		    if (recipients.isEmpty()) {
			return;
		    }
		    Feeds.getService().sendPost(entry, recipients, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
			    // TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(Void result) {
			    // TODO Auto-generated method stub

			}
		    });
		}
	    }
	});
	if (Feeds.isLoggined()) {
	    linkpanel.add(sendLink);
	}

	linkpanel.addStyleName("linkpanel");

	main.add(linkpanel, DockPanel.SOUTH);
	setWidget(main);
    }

    private List<String> checkMailAddresses(String addresses) {
	List<String> result = new ArrayList<String>();
	String[] mails = addresses.split(",");
	if (mails.length == 0) {
	    Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_INVALID_EMAIL, null));
	    return result;
	}
	for (String str : mails) {
	    str = str.trim();
	    boolean ok = EmailValidator.isValid(str);
	    if (ok) {
		result.add(str);
	    } else {
		Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_INVALID_EMAIL + str, null));
	    }
	}
	return result;
    }
}
